﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB.YJKControls;
using SystemDiagramNew.SystemDiagramLegend;
using SystemDiagramNew.TypeDefinition;
using SystemDiagramNew.Utils;

namespace SystemDiagramNew
{
	public partial class SystemDiagramForm : System.Windows.Forms.Form
	{
		private SystemDiagramForm()
		{
			this.Option = SystemDiagramOption.Instance;
			this.InitializeComponent();
		}

		public SystemDiagramForm(ExternalCommandData cmdData, SystemDiagramOption option) : this()
		{
			this.CmdData = cmdData;
			this.Option = option;
		}

		public SystemDiagramOption Option { get; set; }

		public MEPSystem SelectedSystem { get; set; }

		public ExternalCommandData CmdData { get; set; }

		private void SystemDiagramForm_Load(object sender, EventArgs e)
		{
			if (this.InitSystems())
			{
				this.radioBySelectElement.Checked = (this.Option.SelectionMothod == SelectionMothod.ByPickedElement);
				this.radioBySystem.Checked = (this.Option.SelectionMothod == SelectionMothod.BySystem);
				this.UpdateOkButtonState();
			}
			else
			{
				this.radioBySelectElement.Checked = true;
				this.treeViewSystem.Enabled = false;
				this.radioBySystem.Checked = false;
			}
			if (this.Option.SelectionRange == SelectionRange.All)
			{
				this.rbAll.Checked = true;
			}
			else if (this.Option.SelectionRange == SelectionRange.Floor)
			{
				this.rbFloor.Checked = true;
			}
			else
			{
				this.rbBox.Checked = true;
			}
			this.tbAngle.SetValue(this.Option.Angle);
			this.tbScaleX.SetValue(this.Option.ScaleX);
			this.tbScaleY.SetValue(this.Option.ScaleY);
			this.tbScaleZ.SetValue(this.Option.ScaleZ);
			this.tbScaleLegend.SetValue(this.Option.ScaleLegend);
			this.chbLabelPipeDiameter.Checked = this.Option.LabelPipeDiameter;
			this.chbLabelElevation.Checked = this.Option.LabelElevation;
			this.comboBoxScale.Clear();
			if (this.Option.Scale != 50 && this.Option.Scale != 100)
			{
				if (this.Option.Scale < 50)
				{
					this.comboBoxScale.AddItem(this.Option.Scale.ToString());
					this.comboBoxScale.AddItem("50");
					this.comboBoxScale.AddItem("100");
				}
				else if (this.Option.Scale > 50)
				{
					this.comboBoxScale.AddItem("50");
					this.comboBoxScale.AddItem("100");
					this.comboBoxScale.AddItem(this.Option.Scale.ToString());
				}
				else
				{
					this.comboBoxScale.AddItem("50");
					this.comboBoxScale.AddItem(this.Option.Scale.ToString());
					this.comboBoxScale.AddItem("100");
				}
			}
			else
			{
				this.comboBoxScale.AddItem("50");
				this.comboBoxScale.AddItem("100");
			}
			this.comboBoxScale.Text = this.Option.Scale.ToString();
		}

		private bool InitSystems()
		{
			return true;
		}

		private void SystemDiagramNewForm_FormClosing(object sender, FormClosingEventArgs e)
		{
		}

		private void UpdateOkButtonState()
		{
		}

		private void SaveCompornentData()
		{
			if (this.Option == null)
			{
				return;
			}
			if (this.rbAll.Checked)
			{
				this.Option.SelectionRange = SelectionRange.All;
			}
			else if (this.rbFloor.Checked)
			{
				this.Option.SelectionRange = SelectionRange.Floor;
			}
			else
			{
				this.Option.SelectionRange = SelectionRange.Box;
			}
			this.Option.Angle = this.tbAngle.GetValue(this.Option.Angle);
			this.Option.ScaleX = this.tbScaleX.GetValue(this.Option.ScaleX);
			this.Option.ScaleY = this.tbScaleY.GetValue(this.Option.ScaleY);
			this.Option.ScaleZ = this.tbScaleZ.GetValue(this.Option.ScaleZ);
			this.Option.ScaleLegend = this.tbScaleLegend.GetValue(this.Option.ScaleLegend);
			this.Option.LabelPipeDiameter = this.chbLabelPipeDiameter.Checked;
			this.Option.LabelElevation = this.chbLabelElevation.Checked;
			int scale = 50;
			if (int.TryParse(this.comboBoxScale.Text, out scale))
			{
				this.Option.Scale = scale;
			}
			this.Option.Save();
		}

		private void tbScaleX_Validating(object sender, CancelEventArgs e)
		{
			try
			{
				if (Geometry.Lessthan_Or_Equal(double.Parse(this.tbScaleX.Text), 0.0))
				{
					MessageBox.Show("请输入一个大于0的数.", "盈建科提示");
					this.tbScaleX.Text = "1";
					e.Cancel = true;
				}
			}
			catch (FormatException)
			{
				MessageBox.Show("请输入一个大于0的数.", "盈建科提示");
				e.Cancel = true;
			}
		}

		private void tbScaleY_Validating(object sender, CancelEventArgs e)
		{
			try
			{
				if (Geometry.Lessthan_Or_Equal(double.Parse(this.tbScaleY.Text), 0.0))
				{
					MessageBox.Show("请输入一个大于0的数.", "盈建科提示");
					this.tbScaleY.Text = "1";
					e.Cancel = true;
				}
			}
			catch (FormatException)
			{
				MessageBox.Show("请输入一个大于0的数.", "盈建科提示");
				e.Cancel = true;
			}
		}

		private void tbScaleZ_Validating(object sender, CancelEventArgs e)
		{
			try
			{
				if (Geometry.Lessthan_Or_Equal(double.Parse(this.tbScaleZ.Text), 0.0))
				{
					MessageBox.Show("请输入一个大于0的数.", "盈建科提示");
					this.tbScaleZ.Text = "1";
					e.Cancel = true;
				}
			}
			catch (FormatException)
			{
				MessageBox.Show("请输入一个大于0的数.", "盈建科提示");
				e.Cancel = true;
			}
		}

		private void tbScaleLegend_Validating(object sender, CancelEventArgs e)
		{
			try
			{
				if (Geometry.Lessthan_Or_Equal(double.Parse(this.tbScaleLegend.Text), 0.0))
				{
					MessageBox.Show("请输入一个大于0的数.", "盈建科提示");
					this.tbScaleLegend.Text = "1";
					e.Cancel = true;
				}
			}
			catch (FormatException)
			{
				MessageBox.Show("请输入一个大于0的数.", "盈建科提示");
				e.Cancel = true;
			}
		}

		private void tbAngle_Validating(object sender, CancelEventArgs e)
		{
			try
			{
				double val = double.Parse(this.tbAngle.Text);
				if (Geometry.LessThan(val, 0.0) || Geometry.GreaterThan(val, 360.0))
				{
					MessageBox.Show("请输入一个0～360的数.", "盈建科提示");
					this.tbAngle.Text = "45";
					e.Cancel = true;
				}
			}
			catch (FormatException)
			{
				MessageBox.Show("请输入一个0～360的数.", "盈建科提示");
				e.Cancel = true;
			}
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			PipeInfo.BLineReplace = true;
			this.Option.SelectionMothod = (this.radioBySystem.Checked ? SelectionMothod.BySystem : SelectionMothod.ByPickedElement);
			this.Option.SelectionRange = (this.rbAll.Checked ? SelectionRange.All : (this.rbFloor.Checked ? SelectionRange.Floor : SelectionRange.Box));
			this.Option.SelectionMothod = SelectionMothod.ByPickedElement;
			TreeNode selectedNode = this.treeViewSystem.SelectedNode;
			if (this.Option.SelectionMothod == SelectionMothod.BySystem && selectedNode == null)
			{
				MessageBox.Show("请选择一个系统");
				return;
			}
			this.SelectedSystem = ((selectedNode == null) ? null : (selectedNode.Tag as MEPSystem));
			this.SaveCompornentData();
			base.DialogResult = DialogResult.OK;
		}

		private void radioBySystem_CheckedChanged(object sender, EventArgs e)
		{
			this.treeViewSystem.Enabled = this.radioBySystem.Checked;
			this.UpdateOkButtonState();
		}

		private void treeViewSystem_AfterSelect(object sender, TreeViewEventArgs e)
		{
			this.UpdateOkButtonState();
		}

		private void btnLegend_Click(object sender, EventArgs e)
		{
			new LegendMappingForm(this.CmdData)
			{
				Owner = this
			}.ShowDialog();
		}
	}
}
